package edu.hitsz;

import edu.hitsz.application.Main;
import edu.hitsz.scorelist.Score;
import edu.hitsz.scorelist.ScoreDao;
import edu.hitsz.scorelist.ScoreDaoImpl;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.IOException;

public class ScoreRank {
    private JPanel scoreRank;
    private JButton delete;
    private JLabel pattern;
    private JLabel title;
    private JTable scoreTable;
    private JScrollPane tableScrollPanel;
    public static DefaultTableModel model;
    public static ScoreDaoImpl scoreDaoImpl;
    private static Object[][] tableData;
    private String[] columnName = {"名次","玩家名","得分","记录时间"};
    public static boolean isDelete = true;
    public static Object object1 = new Object();

    public JPanel getScoreRank() {
        return scoreRank;
    }

    public ScoreRank() throws IOException {
        if (StartGame.selectPattern == "edu.hitsz.application.EasyGame") {
            this.pattern.setText("难度：简单");
        } else if (StartGame.selectPattern == "edu.hitsz.application.NormalGame") {
            this.pattern.setText("难度：普通");
        } else {
            this.pattern.setText("难度：困难");
        }
        this.pattern.setFont(new Font("宋体",Font.BOLD,25));

        scoreDaoImpl = new ScoreDaoImpl();
        tableData = new Object[ScoreDaoImpl.scores.size()][4];

        model = new DefaultTableModel() {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        update();

        delete.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                int n = JOptionPane.showConfirmDialog(null, "是否确定删除选中的玩家?", "选择一个选项", JOptionPane.YES_NO_OPTION);
                if (n == JOptionPane.YES_OPTION) {
                    int row = scoreTable.getSelectedRow();
                    System.out.println(row);
                    if (row != -1) {
                        model.removeRow(row);
                        try {
                            scoreDaoImpl.delete(row);
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                        System.out.println(ScoreDaoImpl.scores.size());
                        update();
                        try {
                            scoreDaoImpl.getscorelist();
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                    }
                } else if (n == JOptionPane.NO_OPTION) {
                }
            }
        });
    }

    public void update() {
        if (ScoreDaoImpl.scores.size() > 0) {
            tableData = new Object[ScoreDaoImpl.scores.size()][4];
            for (Score player : ScoreDaoImpl.scores) {
                tableData[player.getRank() - 1][0] = player.getRank();
                tableData[player.getRank() - 1][1] = player.getUserName();
                tableData[player.getRank() - 1][2] = player.getScore();
                tableData[player.getRank() - 1][3] = player.getTime();
            }
            model.setDataVector(tableData,columnName);
            scoreTable.setModel(model);
            DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();
            tcr.setHorizontalAlignment(JLabel.CENTER);
            scoreTable.setDefaultRenderer(Object.class,tcr);
            tableScrollPanel.setViewportView(scoreTable);
        }
    }

    private void createUIComponents() {
        // TODO: place custom component creation code here
    }
}
